﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using webnew.Common;
using webnew.BLL;
namespace webnew.Systems
{
    public partial class S_userList : ListBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            _ListTbName = "Sys_User";       //对应的数据库表名
            _EditSrc = "S_userEdit.aspx";    //编辑页面
            _DelType = DeleteType.FalseDel;  //逻辑删除
            _Grdview = grdView;
            #region 传递参数，把控件值赋值给参数
            _SrcPrams = "&UserType=" + ddlUserType.SelectedValue + "&CompId=" + UcCompany1.SelectedValue + "&DeptId=" + ddlOADept.SelectedValue + "&TrueName=" + txtTrueName.Text;
            _SrcPrams += "&TitleName=" + txtTitleName.Text + "&Status=" + dllStatus.SelectedValue +"&RecordCount=" + AspNetPager1.RecordCount + "&CurrentPageIndex=" + AspNetPager1.CurrentPageIndex;
            #endregion
            btnExcelVisble = true; //设置导出不可见
            if (!IsPostBack)
            {
                txtOADept.Style.Add("visibility", "hidden");  //隐藏选中的OA部门ID
                txtHRDept.Style.Add("visibility", "hidden");  //隐藏选中的HR部门ID
                SetTitle(true, "后台管理 >> 用户管理");
                //绑定用户类型数据
                webnew.Common.CommonHelper.DataDdlBind(ddlUserType, new BLL.Sys_UserType().GetList(), "UserTypeName", "id");             
                //绑定OA单位列表,初始化部门列表
                UcCompany1.show(true, "ALL");
                ////绑定HR单位列表,初始化部门列表
                //UcCompany2.show(true, "HR");
                //初始化OA部门和HR部门             
                ddlOADept.Items.Insert(0, new ListItem("--请选择--", "0"));
                //ddlHRDept.Items.Insert(0, new ListItem("--请选择--", "0"));

                #region 接收参数，并赋值给检索控件
                if (Request.QueryString["UserType"] != null) ddlUserType.SelectedValue = Request.QueryString["UserType"].ToString();
                if (Request.QueryString["CompId"] != null) UcCompany1.SelectedValue = Request.QueryString["CompId"].ToString();
                if (Request.QueryString["DeptId"] != null) ddlOADept.SelectedValue = Request.QueryString["DeptId"].ToString();
                if (Request.QueryString["TrueName"] != null) txtTrueName.Text = Request.QueryString["TrueName"].ToString();
                if (Request.QueryString["TitleName"] != null) txtTitleName.Text = Request.QueryString["TitleName"].ToString();
                if (Request.QueryString["Status"] != null) dllStatus.SelectedValue = Request.QueryString["Status"].ToString();
                //记录分页页数 需传递总条数和当前页数 
                if (Request.QueryString["RecordCount"] != null) AspNetPager1.RecordCount = Convert.ToInt32(Request.QueryString["RecordCount"]);
                if (Request.QueryString["CurrentPageIndex"] != null) AspNetPager1.CurrentPageIndex = Convert.ToInt32(Request.QueryString["CurrentPageIndex"]);
                #endregion
                //查询              
                AspNetPager1_PageChanged(null, null);
            }
        }
       
               
        /// <summary>
        /// 通过单位ID获取部门列表
        /// </summary>
        [WebMethod]
        public static List<Model.Sys_DeptInfo> GetDeptList(string CompID)
        {
            return new BLL.Sys_Dept().GetDeptListByComp(CompID);
        }

        /// <summary>
        /// 检索数据
        /// </summary>       
        public void Search()
        {
           
            //关联的数据库表名
            System.Text.StringBuilder strtable = new System.Text.StringBuilder();
            //要查询的字段
            System.Text.StringBuilder strFeild = new System.Text.StringBuilder();
            //分页的查询的条件 
            System.Text.StringBuilder _conditon = new System.Text.StringBuilder();
            string strsort = "";
            switch (ddlUserType.SelectedItem.Text)
            {
                case "公司用户":
                case "油库用户":
                    strFeild.Append(" g.UserTypeName,f.*,b.compname as OACompName,(select deptname from Sys_dept where id=a.OADeptId) as OADeptName ");
                    strFeild.Append(" ,a.*  ");
                    strtable.Append(" Sys_Staff a join Sys_Company b on a.OACompId=b.Id  ");                  
                    //strtable.Append(" join Sys_Company d on a.HRCompId=d.Id join Sys_Dept e on a.HRDeptId=e.Id  ");
                    strtable.Append(" join Sys_User f on f.StaffId=a.Id ");
                    strtable.Append(" join Sys_UserType g on  f.UserTypeId=g.Id ");
                    //用户姓名,职称检索条件
                    _conditon.Append(" f.truename like '%" + txtTrueName.Text.Trim().Replace("'", "") + "%'");
                    _conditon.Append(" and a.titleName like '%" + txtTitleName.Text.Trim().Replace("'", "") + "%'");
                    //用户类型检索条件
                    _conditon.Append(" and f.UserTypeId ='" + ddlUserType.SelectedValue + "'");
                    //OA公司,部门检索条件
                    if (!(UcCompany1.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.OACompId ='" + UcCompany1.SelectedValue + "'");
                    if (!(txtOADept.Text == "0")) _conditon.Append(" and a.OADeptId ='" + txtOADept.Text + "'");
                    ////HR公司,部门检索条件
                    //if (!(UcCompany2.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.HRCompId ='" + UcCompany2.SelectedValue + "'");
                    //if (!(txtHRDept.Text == "0")) _conditon.Append(" and a.HRDeptId ='" + txtHRDept.Text + "'");
                    //状态检索条件
                    if (!(dllStatus.SelectedValue == "全部"))
                    {
                        _conditon.Append(" and f.isdel ='" + dllStatus.SelectedValue + "'");
                    }
                    else _conditon.Append(" and f.isdel <> 1");
                    strsort = "b.compname,(select deptname from Sys_dept where id=a.OADeptId),UserTypeName,a.Id";
                    break;
                default:               
                    strFeild.Append(" b.UserTypeName ,a.* ");
                    strtable.Append(" Sys_User a join Sys_UserType b on  a.UserTypeId=b.Id ");
                    //用户姓名检索条件
                    _conditon.Append(" a.truename like '%" + txtTrueName.Text.Trim().Replace("'", "") + "%'");
                    //用户类型检索条件
                    _conditon.Append(" and a.UserTypeId ='"+ ddlUserType.SelectedValue +"'");
                    //状态检索条件
                    if (!(dllStatus.SelectedValue == "全部"))
                    {
                        _conditon.Append(" and a.isdel ='" + dllStatus.SelectedValue + "'");
                    }
                    else _conditon.Append(" and a.isdel <> 1");
                    strsort = "a.truename,a.id";
                    break;                 
            }
         
            //获取符合条件的用户列表总数
            AspNetPager1.RecordCount = new BLL.Sys_User().GetCount(strtable.ToString(), _conditon.ToString());
            // 如果是公司用户,油库用户显示单位部门.若不是,不显示单位部门
            changeGrdViewCell();
            //获取符合条件的用户列表，并绑定显示
            webnew.Common.CommonHelper.DataGrdBind(grdView, new BLL.Sys_User().GetPageList(strFeild.ToString(), _conditon.ToString(), grdView.PageSize, AspNetPager1.CurrentPageIndex, strtable.ToString(), strsort, true));
        }


        /// <summary>
        /// 重写导出事件;
        /// </summary>
        protected override void _Excel()
        {
            //关联的数据库表名
            System.Text.StringBuilder strtable = new System.Text.StringBuilder();
            //要查询的字段
            System.Text.StringBuilder strFeild = new System.Text.StringBuilder();
            //分页的查询的条件 
            System.Text.StringBuilder _conditon = new System.Text.StringBuilder();
            switch (ddlUserType.SelectedItem.Text)
            {
                case "公司用户":
                case "油库用户":
                    strFeild.Append(" g.UserTypeName,f.*,b.compname as OACompName,(select deptname from Sys_dept where id=a.OADeptId) as OADeptName ");
                    strFeild.Append(" ,a.*  ");
                    strtable.Append(" Sys_Staff a join Sys_Company b on a.OACompId=b.Id ");
                    //strtable.Append(" join Sys_Company d on a.HRCompId=d.Id join Sys_Dept e on a.HRDeptId=e.Id  ");
                    strtable.Append(" join Sys_User f on f.StaffId=a.Id ");
                    strtable.Append(" join Sys_UserType g on  f.UserTypeId=g.Id ");
                    //用户姓名,职称检索条件
                    _conditon.Append(" f.truename like '%" + txtTrueName.Text.Trim().Replace("'", "") + "%'");
                    _conditon.Append(" and a.titleName like '%" + txtTitleName.Text.Trim().Replace("'", "") + "%'");
                    //用户类型检索条件
                    _conditon.Append(" and f.UserTypeId ='" + ddlUserType.SelectedValue + "'");
                    //OA公司,部门检索条件
                    if (!(UcCompany1.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.OACompId ='" + UcCompany1.SelectedValue + "'");
                    if (!(txtOADept.Text == "0")) _conditon.Append(" and a.OADeptId ='" + txtOADept.Text + "'");
                    ////HR公司,部门检索条件
                    //if (!(UcCompany2.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.HRCompId ='" + UcCompany2.SelectedValue + "'");
                    //if (!(txtHRDept.Text == "0")) _conditon.Append(" and a.HRDeptId ='" + txtHRDept.Text + "'");
                    //状态检索条件
                    if (!(dllStatus.SelectedValue == "全部"))
                    {
                        _conditon.Append(" and f.isdel ='" + dllStatus.SelectedValue + "'");
                    }
                    else _conditon.Append(" and f.isdel <> 1");
                    _conditon.Append(" order by b.compname,(select deptname from Sys_dept where id=a.OADeptId),UserTypeName,a.Id");
                    break;
                default:
                    strFeild.Append(" b.UserTypeName ,a.* ");
                    strtable.Append(" Sys_User a join Sys_UserType b on  a.UserTypeId=b.Id ");
                    //用户姓名检索条件
                    _conditon.Append(" a.truename like '%" + txtTrueName.Text.Trim().Replace("'", "") + "%'");
                    //用户类型检索条件
                    _conditon.Append(" and a.UserTypeId ='" + ddlUserType.SelectedValue + "'");
                    //状态检索条件
                    if (!(dllStatus.SelectedValue == "全部"))
                    {
                        _conditon.Append(" and a.isdel ='" + dllStatus.SelectedValue + "'");
                    }
                    else _conditon.Append(" and a.isdel <> 1"); 
                    _conditon.Append(" order by a.truename,a.id");
                    break;
            }
            //ToExcelconditon.Append(" isdel=0 and titleName like '%" + txtTitleName.Text.Trim().Replace("'", "") + "%'");
            //_conditon.Append(" order by UserTypeName desc ");
            string strHeaderText = "用户管理"; //导出的Excel标题
            string filename = "用户管理";      //Excel文件名 不包括后缀名
            webnew.Common.CommonHelper.DataGrdBind(grdExcel, new BLL.Sys_User().GetExcelList(strFeild.ToString(),strtable.ToString(), _conditon.ToString()));
            ToExcel.GridViewToExcel(grdExcel, strHeaderText, filename);
        }


        /// <summary>
        /// 重写删除事件,使删除后刷新页面;
        /// </summary>
        protected override void _Del()
        {
            //----------系统日志model

            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.UserID = UserInfor.Id;                  //用户ID
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间

            string error = "";                   //获取错误信息

            try
            {
                base._Del();
            }
            catch (Exception e)
            {
                error = e.ToString();
            }
            finally
            {
                //添加一条系统日志
                new BLL.Sys_Log().AddSysLog(error, "删除", modelLog, "系统用户", "用户，用户名为" + GetNames("loginid", _ListTbName));
            }
            AspNetPager1_PageChanged(null, null);
        }

        /// <summary>
        /// 检索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1_PageChanged(null, null);
        }

        /// <summary>
        /// 分页控件事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Search();
            //刷新后重新绑定之前所选的部门
            //webnew.Common.CommonHelper.DataDdlBind(ddlDept, GetDeptList(ddlComp.SelectedValue), "deptname", "ID");
            //ddlDept.Items.Insert(0, new ListItem("所有部门", "0"));            
            //webnew.Common.CommonHelper.DdlFind(ddlDept, txtDept.Text);
        }


        protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标经过变色
                e.Row.Attributes.Add("onmouseover", "GridViewMouseover(this)");
                e.Row.Attributes.Add("onmouseout", "GridViewMouseout(this)");
                e.Row.Attributes.Add("onclick", "GridViewonclick(this)");
                //转换状态
                e.Row.Cells[7].Text = e.Row.Cells[7].Text == "0" ? "启用" : "停用";
                //备注提示
                e.Row.Cells[8].ToolTip = e.Row.Cells[8].Text == "&nbsp;" ? "" : e.Row.Cells[8].Text;
               
           
            }
        }

        /// <summary>
        /// 如果是公司用户,油库用户显示单位部门.若不是,不显示单位部门
        /// </summary>
        private void changeGrdViewCell()
        {
            switch (ddlUserType.SelectedItem.Text)
            {
                case "公司用户":
                case "油库用户":
                    grdView.Columns[4].Visible = true;
                    grdView.Columns[5].Visible = true;
                    
                    break;
                default:
                    grdView.Columns[4].Visible = false;
                    grdView.Columns[5].Visible = false;
                    
                    break;
            }
        }

        protected void grdExcel_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               
               //转换状态
                e.Row.Cells[6].Text = e.Row.Cells[6].Text == "0" ? "启用" : "停用";
                
            }
        }
   

      
       
    }
}
